<!--电子围栏页 GeofenceView.vue-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofenceMap')">新建围栏
    </el-button>
  </div>
  <!-- 围栏查询卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchGeofence.name"
                  @keydown.enter="loadGeofence"></el-input>
      </el-form-item>
      <el-form-item label="围栏状态" style="width:290px;">
        <el-select placeholder="请选择" v-model="searchGeofence.status"
                   @change="loadGeofence">
          <el-option label="启用" value="1"/>
          <el-option label="禁用" value="0"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadGeofence">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 围栏表格数据 -->
  <el-card style="margin:20px;">
    <el-table stripe :data="geofenceArr">
      <el-table-column label="编号" prop="id" type="index" align="center" width="100"></el-table-column>
      <el-table-column label="围栏名称" prop="name" align="center"></el-table-column>
      <el-table-column label="围栏坐标点" prop="position" align="center" show-overflow-tooltip="true"
                       width="500"></el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
      <el-table-column label="围栏状态" align="center">
        <template #default="scope">
          <el-switch inactive-value="0" active-value="1" v-model="scope.row.status"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="editVehicle(scope.row.id)">管理车辆</el-button>
          <el-button type="primary" link @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 已绑定车辆弹窗 -->
  <el-dialog title="已绑定车辆" v-model="bindVehicleDialogVisible" style="padding:40px;">
    <el-button type="primary" style="margin:10px 0;" @click="showUnbindVehicleList">添加绑定车辆</el-button>
    <el-table :data="bindVehicleArr">
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column
          label="操作"
          fixed="right"
          width="200"
          align="center">
        <template #default="scope">
          <el-button link type="primary" @click="removeGeofence(scope.row.id)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加可绑定车辆列表弹窗 -->
  <el-dialog title="可绑定车辆列表" v-model="AddBindVehicleDialogVisible">
    <el-table :data="unBindVehicleArr">
      <el-table-column label="编号" type="index" align="center" width="80px"></el-table-column>
      <el-table-column prop="brand" label="车辆品牌" align="center"></el-table-column>
      <el-table-column prop="license" label="车牌号" align="center"></el-table-column>
      <el-table-column prop="type" label="车辆类型" align="center"></el-table-column>
      <el-table-column prop="status" label="车辆状态" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="addGeofence(scope.row.id)">绑定</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";

//控制"已绑定车辆弹窗"是否显示
const bindVehicleDialogVisible = ref(false);
//控制"可绑定车辆列表"是否显示
const AddBindVehicleDialogVisible = ref(false);

//定义对象保存搜索条件
const searchGeofence = ref({
  name: '',
  status: ''
})

//定义数组保存查询道德围栏信息
const geofenceArr = ref([])

//定义方法查询围栏信息
const loadGeofence = () => {
  console.log(searchGeofence.value);
  let data = qs.stringify(searchGeofence.value);
  console.log(data)
  axios.get(BASE_URL + '/v1/geofence/select?' + data)
      .then((response) => {
        if (response.data.code === 2000) {
          geofenceArr.value = response.data.data;
          console.log(geofenceArr.value)
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

//将查询围栏信息的方法挂在到页面上
onMounted(() => {
  loadGeofence();
})

//定义重置搜索的方法
const resetSearch = () => {
  searchGeofence.value = {}
  loadGeofence()
}


//定义数组保存信息当前围栏已绑定车辆信息
const bindVehicleArr = ref([]);

//定义全局变量保存当前围栏id
let geoId;

//定义方法加载已绑定车辆弹窗
const editVehicle = (geofenceId) => {
  console.log(geofenceId)
  geoId = geofenceId;
  //显示已绑定车辆弹窗
  bindVehicleDialogVisible.value = true;
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceId=' + geofenceId).then((response) => {
    if (response.data.code === 2000) {
      bindVehicleArr.value = response.data.data;
      console.log(bindVehicleArr.value)
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义方法移除围栏
const removeGeofence = (vehicleId) => {
  console.log(vehicleId)
  if (confirm('确定要移除吗？')) {
    axios.post(BASE_URL + '/v1/vehicle/unbind/' + vehicleId)
        .then((response) => {
          if (response.data.code === 2000) {
            ElMessage.success('移除成功!');
            editVehicle(geoId);
          } else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}

//定义数组保存可绑定车辆列表
const unBindVehicleArr = ref([]);

//定义方法加载可绑定车辆列表弹窗
const showUnbindVehicleList = () => {
  AddBindVehicleDialogVisible.value = true;
  axios.get(BASE_URL + '/v1/vehicle/select?geofenceBindStatus=0')
      .then((response) => {
        if (response.data.code === 2000) {
          unBindVehicleArr.value = response.data.data;
          console.log(unBindVehicleArr.value);
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

//定义方法添加绑定车辆
const addGeofence = (vehicleId) => {
  console.log(vehicleId);
  axios.post(BASE_URL+'/v1/vehicle/bind/'+geoId+'/'+vehicleId)
      .then((response) => {
        if (response.data.code === 2000) {
          ElMessage.success('绑定成功!');
          AddBindVehicleDialogVisible.value = false;
          editVehicle(geoId);
        } else {
          ElMessage.error(response.data.msg);
        }
      })
}

//定义方法删除围栏详情
const deleteVehicle = (id) => {
  console.log(id)
  if (confirm('确定要删除吗？')) {
    axios.post(BASE_URL + '/v1/geofence/delete/' + id)
        .then((response) => {
          if (response.data.code === 2000) {
            ElMessage.success('删除成功!');
            loadGeofence();
          } else {
            ElMessage.error(response.data.msg);
          }
        })
  }
}

</script>

<style scoped>
</style>